package cn.hrlly.tao2shou.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.hrlly.tao2shou.R;
import cn.hrlly.tao2shou.adapter.HistoryAdapter;
import cn.hrlly.tao2shou.domain.Commodities;
import cn.hrlly.tao2shou.other.Config;
import cn.hrlly.tao2shou.presenter.ShowHistoryCommoditiesPresenter;
import cn.hrlly.tao2shou.presenter.ShowHistoryCommoditiesPresenterImpl;
import cn.hrlly.tao2shou.view.base.BaseActivity;
import cn.hrlly.tao2shou.view.view_interface.ShowHistoryCommoditiesView;

public class HistoryActivity extends BaseActivity implements ShowHistoryCommoditiesView{

    @Bind(R.id.lvHistory)
    ListView lvHistory;
    private ShowHistoryCommoditiesPresenter historyCommoditiesPresenter;
    private ArrayList<Commodities> list;
    private HistoryAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        ButterKnife.bind(this);
        init();
        clickListview();
    }

    //关闭数据库
    @Override
    protected void onDestroy() {
        super.onDestroy();
        historyCommoditiesPresenter.closeDataBase();
    }

    //初始化
    public void init(){
        list=new ArrayList<Commodities>();
        historyCommoditiesPresenter=new ShowHistoryCommoditiesPresenterImpl(this,this);
        historyCommoditiesPresenter.showHistoryCommodities();
    }

    //listview点击事件
    public void clickListview(){
        lvHistory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(HistoryActivity.this,CommodityDetailsActivity.class);
                intent.putExtra(Config.key_release_id,list.get(position).getReleaseId());
                startActivity(intent);
            }
        });
    }

    //对activity的操作
    @Override
    public void showListView(ArrayList<Commodities> list) {
        list.addAll(list);
        adapter=new HistoryAdapter(list,this);
        lvHistory.setAdapter(adapter);
    }
}
